package com.gobygang.client.uibinder;

import java.util.HashMap;

import com.gobygang.client.Authenication.Provider;
import com.gobygang.client.GobygangProcessor;
import com.gobygang.client.PanelType;
import com.gobygang.client.dialog.ProcessingDialog;
import com.gobygang.client.facebook.FacebookApi;
import com.gobygang.client.map.MapView;
import com.gobygang.client.map.NavigatorManager;
import com.gobygang.client.map.SearchView;
import com.gobygang.client.ui.ButtonType;
import com.gobygang.shared.TaxiGangInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class GobygangRoute extends Composite implements MapView  {

	private static GobygangRouteUiBinder uiBinder = GWT
	.create(GobygangRouteUiBinder.class);

	interface GobygangRouteUiBinder extends
	UiBinder<Widget, GobygangRoute> {
	}

	private HashMap<PanelType,GobygangPanel> taxiGangPanels;

	public GobygangRoute() {
		initWidget(uiBinder.createAndBindUi(this));
		//mapViewWidget = new NavigatorWidget(searchAndCreate);
		taxiGangPanels = new HashMap<PanelType, GobygangPanel>();
		taxiGangPanels.put(PanelType.SEARCH,searchAndCreate);
		taxiGangPanels.put(PanelType.FEEDBACK,feedback);
		taxiGangPanels.put(PanelType.LOGIN,login);
		taxiGangPanels.put(PanelType.CREATE_STEP1,createStep1);
		taxiGangPanels.put(PanelType.CREATE_STEP2A,createStep2a);
		taxiGangPanels.put(PanelType.CREATE_STEP2B,createStep2b);
		taxiGangPanels.put(PanelType.CREATE_STEP3,createStep3);
		taxiGangPanels.put(PanelType.CREATE_STEP4,createStep4);
		taxiGangPanels.put(PanelType.CONGRATULATIONS_CREATE,congratulationsCreate);
		taxiGangPanels.put(PanelType.CONGRATULATIONS_JOIN,congratulationsJoin);
		taxiGangPanels.put(PanelType.JOIN,congratulationsJoin);
		taxiGangPanels.put(PanelType.SORRY,sorry);
		taxiGangPanels.put(PanelType.MYTAXIGANGS,myTaxiGangs);
		taxiGangPanels.put(PanelType.SUGGESTION,suggestion);
		taxiGangPanels.put(PanelType.MESSAGES,messages);
		taxiGangPanels.put(PanelType.NEW_MESSAGE,newMessage);
		taxiGangPanels.put(PanelType.MOBILENUMBER,mobilenumber);
		taxiGangPanels.put(PanelType.AWARDS,awards);

		for (GobygangPanel p : taxiGangPanels.values()){
			p.setTaxiGangRoute(this);
		}

		searchAndCreate.setJoinButton(suggestion.getJoinButton());
		
		Command cmd = null;
		final SearchView searchView = getSearchView();


		NavigatorManager.executeNavigator(null,searchAndCreate, taxiGangMapPanel);
		//no partnerm && no suggestion





	}

	private PanelType panelToShowAtShowLogin = PanelType.SEARCH;

	//private NavigatorWidget mapViewWidget;

	@UiField
	TaxiGangHeader taxiGangHeader;

	@UiField
	SearchAndCreate searchAndCreate;

	@UiField
	Feedback feedback;

	@UiField
	Login login;

	@UiField Sorry sorry;
	@UiField MyTaxiGangs myTaxiGangs;
	@UiField Suggestion suggestion;
	@UiField Messages messages;
	@UiField NewMessage newMessage;

	@UiField CreateStep1 createStep1;
	@UiField CreateStep2a createStep2a;
	@UiField CreateStep2b createStep2b;
	@UiField CreateStep3 createStep3;
	@UiField CreateStep4 createStep4;
	@UiField CongratulationsCreate congratulationsCreate;
	@UiField CongratulationsJoin congratulationsJoin;
	@UiField Mobilenumber mobilenumber;
	@UiField Awards awards;


	@UiField
	TaxiGangMapPanel taxiGangMapPanel;




	@Override
	public void resetSuggestion() {
		NavigatorManager.resetSuggestion(searchAndCreate, taxiGangMapPanel);
	}

	@Override
	public void setPanelToShowOnLogin(PanelType panelType) {
		this.panelToShowAtShowLogin = panelType;

	}

	@Override
	public void createStartMarker(String start, boolean setAddress) {
		NavigatorManager.createStartMarker(start, searchAndCreate, taxiGangMapPanel, setAddress);

	}

	@Override
	public void createDestinationMarker(String destination, boolean setAddress) {
		NavigatorManager.createDestinationMarker(destination,searchAndCreate, taxiGangMapPanel, setAddress, null);

	}


	@Override
	public void createStartDestinationMarker(String start, String dest, boolean setAddress) {
		NavigatorManager.createStartDestinationMarker(start, dest,searchAndCreate, taxiGangMapPanel, setAddress);

	}

	@Override
	public void showSuggestionInMap(TaxiGangInfo suggestion) {
		NavigatorManager.createSuggestion(suggestion,searchAndCreate, taxiGangMapPanel, false);
	}

	@Override
	public void showMyTaxiGangInMap(TaxiGangInfo suggestion, boolean setAddress) {
		NavigatorManager.createStartDestinationMarker(suggestion.getStart(),suggestion.getDestination(),searchAndCreate, taxiGangMapPanel, setAddress);

	}

	@Override
	public HasClickHandlers getButton(ButtonType buttonType) {
		//		HasClickHandlers hasClickHandlers=null;
		//		switch (buttonType){
		//		//case Feedback: hasClickHandlers = taxiGangHeader.getFeedbackButton(); break;
		//		//case Sigin: hasClickHandlers = taxiGangHeader.getLoginButton(); break;
		//		default:
		//			throw new RuntimeException("unrecognised:"+buttonType);
		//		}
		//return hasClickHandlers;
		return null;
	}



	@Override
	public void setLoggedOut(String msg) {
		show(PanelType.LOGIN);
		taxiGangHeader.showLoginButton(true);
	}

	@Override
	public void showLoginButton(boolean show){
		taxiGangHeader.showLoginButton(show);
	}

	@Override
	public void setLoggedIn(Provider provider) {
		if (provider == Provider.Facebook){
			GobygangProcessor.signOutLink.addClickHandler(new ClickHandler() {						
				@Override
				public void onClick(ClickEvent event) {
					FacebookApi.fbLogout();							
				}
			});
		}
		if (panelToShowAtShowLogin!=null){
			show(panelToShowAtShowLogin);
		}

	}



	@Override
	public void showMap() {
		//NavigatorManager.showMap(taxiGangMapPanel);

	}

	@Override
	public void show(PanelType panelType) {

		ProcessingDialog.hide();
		for (GobygangPanel p : taxiGangPanels.values()){
			p.hide();
		}
		taxiGangPanels.get(panelType).show();
	}

	@Override
	public SearchView getSearchView() {
		return searchAndCreate;
	}


	public enum CreateMode {CALL_A_TAXI,MEET_AT_RANK}
	private CreateMode createMode;
	public CreateMode getCreateMode() {
		return createMode;
	}
	public void setCreateMode(CreateMode createMode) {
		this.createMode=createMode;
	}

	public void showMenu() {
		taxiGangHeader.showMenu();

	}

//	@Override
//	public HasClickHandlers getJoinButton() {
//		return suggestion.getJoinButton();
//	}

	@Override
	public void setFeedbackButtonVisible(boolean b) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setLoginButtonVisible(boolean b) {
		Window.alert("not implemented");
		
	}

}
